﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Security;

namespace ADUSWebApp.Models
{
    public class LoginModel
    {
        /// <summary>
        /// 检查用户是否成功登陆
        /// </summary>
        [Required(ErrorMessage="用户名不能为空")]
        [DisplayName("用户名")]
        public string UserName { get; set; }

        [Required(ErrorMessage = "密码不能为空")]
        [DataType(DataType.Password)]
        [DisplayName("密码")]
        public string UserPwd { get; set; }

        [DisplayName("记住我?")]
        public bool Remember { get; set; }
    }

    public interface IFormsApplyService
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="createPersistentCookie"></param>
        void SignIn(string userName, bool createPersistentCookie);

        /// <summary>
        /// 注销
        /// </summary>
        void SignOut();

    }

    public class FormsApplyService : IFormsApplyService
    {
        #region IFormsAuthenticationService 成员

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="createPersistentCookie"></param>
        public void SignIn(string userName, bool createPersistentCookie)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("值不能为 null 或为空。", "userName");

            FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
        }

        /// <summary>
        /// 注销
        /// </summary>
        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }

        #endregion
    }
}